<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of RegisterController
 *
 * @author asmaa
 */
class RegisterController extends Zend_Controller_Action {

    public function init() {
        require_once 'Zend/Session.php';
    }

    public function indexAction() {
        $form = new APP_Form_UserForm();
        $request = $this->getRequest();

        if ($request->isPost()) {
            if ($form->isValid($_POST)) {

                $date = new Zend_Db_Expr('CURDATE()');

                $users = new Zend_Db_Table('users');
                $new = $users->createRow();
                $new->user_name = $_POST['userName'];
                $new->email = $_POST['mail'];
                $new->job = $_POST['job'];
                $new->birthday = $_POST['bdate'];
                $new->address = $_POST['add'];
                $new->country = $_POST['country'];
                $new->created_at = $date;
                $new->updated_at = $date;
                $new->credit_limit = $_POST['crdtLmt'];
                $new->creadit_card_number = $_POST['crdtNum'];
                $new->save();

                $salt = rand(0, 32000);



                $pswdVal = $_POST['pswd'];
                $concat = $pswdVal . $salt;
                $key = SHA1($concat);

                $pswd = $key;

                $login = new Zend_Db_Table('login');
                $new2 = $login->createRow();
                $new2->email = $_POST['mail'];
                $new2->password = $pswd;
                $new2->salt = $salt;
                $new2->date_created = $date;
                $new2->role = "member";
                $new2->save();


                //send mail

                require_once('Zend/Mail.php');
                require_once('Zend/Mail/Transport/Smtp.php');
                $config = array('auth' => 'login',
                    'username' => 'electronica.website@gmail.com',
                    'password' => 'electronica_123',
                    'port' => 465,
                    'ssl' => 'ssl');


                $transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);
                Zend_Mail::setDefaultTransport($transport);



                $mail = new Zend_Mail();
                $mail->setBodyText("kindly press on the below url to be activated:
            http://electronica/auth/index/param/$salt");
                $mail->setFrom('electronica.website@gmail.com', 'electronica project');
                $mail->addTo('electronica.website@gmail.com', 'Mohammed Zeiada');
                $mail->setSubject('Zend_Mail_Test');
                $mail->send();
                $this->_helper->redirector('notactive');
            }
        }

        $this->view->form = $form;
    }

    public function notactiveAction() {

    }

}

?>
